<?php
$user['spolid'] = $_SESSION['bmb_spolid'];
$dni = array('ned','pon','uto','str','štv','pia','sob');
$mesiace = array('','Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December');

$dnes = date("U",mktime(0,0,0,date("m"),date("d"),date("Y")));
$pocetTyzdnov = ($dnes - $bodNula) / (7*24*60*60);
$pocetTyzdnov = (int) $pocetTyzdnov;
$pocetTyzdnov ++;
$aktualnyTyzden = $pocetTyzdnov;
$pocetTyzdnov += 10;
if (isset($_POST['mesiac'])) {
    $mesiac = $_POST['mesiac'];
} else {
    $mesiac = date("n:Y");
}
echo '<form style="margin: 10px 0; border-bottom: 1px solid gray; padding-bottom: 2px;" action="home.php?akc=77" method="post" id="kalendar">';
$formID = "'kalendar'";
echo '<select name="mesiac" size="1" onchange="submitForm('.$formID.')">';
$temp = '';
for ($i=$pocetTyzdnov;$i>0;$i--) {
    if ($temp!=date("M-Y",($bodNula + ($i-1)*(7*24*60*60)))) {
        if (date("n:Y",($bodNula + ($i-1)*(7*24*60*60)))==$mesiac)
            echo '<option selected value="'.date("n:Y",($bodNula + ($i-1)*(7*24*60*60))).'">'.date("M-Y",($bodNula + ($i-1)*(7*24*60*60))).'</option>';
        else
            echo '<option value="'.date("n:Y",($bodNula + ($i-1)*(7*24*60*60))).'">'.date("M-Y",($bodNula + ($i-1)*(7*24*60*60))).'</option>';
        $temp = date("M-Y",($bodNula + ($i-1)*(7*24*60*60)));
    }
}
echo '</select>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: #cecece">Víkend</span>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: yellow">Sviatok</span>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: green">Manažér</span>';
echo '&nbsp;&nbsp;* Nočných za mesiac, Sviatkov za mesiac, Nadčas za rok = počet celých odpracovaných hodín';
echo '</form>';

$datum = explode(":", $mesiac);
$pocet_dni = date("t",mktime(0,0,0,$datum[0],1,$datum[1]));
?>
<table style="border-collapse: collapse; font-size: 10px; text-align: center; vertical-align: middle">
    <tr>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 100px;"><b><?php echo $mesiace[date("n",mktime(0,0,0,$datum[0],1,$datum[1]))]." ".date("Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 60px;" rowspan="2"><b>Strava za <?php echo date("n/Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 60px;" rowspan="2"><b>Mod za <?php echo date("n/Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 60px;" rowspan="2"><b>Nočné za <?php echo date("n/Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 60px;" rowspan="2"><b>Sviatky za <?php echo date("n/Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 60px;" rowspan="2"><b>Nadčas za <?php echo date("Y",mktime(0,0,0,$datum[0],1,$datum[1])) ?></b></td>
        <?php
        for ($i=1;$i<$pocet_dni+1;$i++) {
            echo '<td rowspan="2" style="background: #eeeeee;height: 15px; width: 25px; border: 1px solid black;"><b>'.$i.'</b></td>';
        }
        ?>
    </tr>
    <tr>
        <td style="background: #cecece;border: 1px solid black;height: 15px; width: 100px;"><b>Meno</b></td>
    </tr>
    <?php
    $zam_s = "SELECT * FROM z WHERE stavid=1 AND spolid='".$user['spolid']."' ";
    $zam_q = MySQL_Query ($zam_s);
    while ($zam = MySQL_Fetch_Array($zam_q)) {
        ?>
    <tr>
            <?php
            if ($zam['manager']!=0) {
                ?>
        <td style="background: green;border: 1px solid black;height: 15px; width: 100px;"><?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?></td>
                <?php
            } else {
                ?>
        <td style="background: white;border: 1px solid black;height: 15px; width: 100px;"><?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?></td>
                <?php
            }
            ?>
        <td style="background: #eee;border: 1px solid black;height: 15px; ">
                <?php
                $vypis = 0;
                $nac_s = "SELECT kolko FROM falkstrava WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array ($nac_q);
                $vypis += $nac['kolko'];
                echo $vypis;
                ?>
        </td>
        <td style="background: #eee;border: 1px solid black;height: 15px; ">
                <?php
                $vypis = 0;
                $nac_s = "SELECT kolko FROM falkmod WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array ($nac_q);
                $vypis += $nac['kolko'];
                echo $vypis;
                ?>
        </td>
        <td style="background: #eee;border: 1px solid black;height: 15px; ">
                <?php
                $vypis = 0;
                $nac_s = "SELECT kolko FROM falknoc WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array ($nac_q);
                $vypis += $nac['kolko'];
                $vypis = $vypis / 3600;
                $vypis = (int) $vypis;
                echo $vypis;
                ?>
        </td>
        <td style="background: #eee;border: 1px solid black;height: 15px; ">
                <?php
                $vypis = 0;
                $nac_s = "SELECT kolko FROM falksviatok WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array ($nac_q);
                $vypis += $nac['kolko'];
                $vypis = $vypis / 3600;
                $vypis = (int) $vypis;
                echo $vypis;
                ?>
        </td>
        <td style="background: #eee;border: 1px solid black;height: 15px; ">
                <?php
                $vypis = 0;
                $nac_s = "SELECT kolko FROM falknadcas WHERE zid = '".$zam['id']."' AND rok='".$datum[1]."'";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array ($nac_q);
                $vypis += $nac['kolko'];
                $vypis = $vypis / 3600;
                $vypis = (int) $vypis;
                echo $vypis;
                ?>
        </td>
            <?php
            for ($i=1;$i<$pocet_dni+1;$i++) {
                if ((date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==0)||(date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==6)) {
                    echo '<td rowspan="1" style="background: #cecece;height: 15px; width: 25px; border: 1px solid black;">';
                } elseif (jeSviatok(date("U",mktime(0,0,0,$datum[0],$i,$datum[1])))) {
                    echo '<td rowspan="1" style="background: yellow;height: 15px; width: 25px; border: 1px solid black;">';
                } else {
                    echo '<td rowspan="1" style="background: white;height: 15px; width: 25px; border: 1px solid black;">';
                }
                $nac_s = "SELECT * FROM dochadzkazamfalk WHERE date='".(mktime(0,0,0,$datum[0],$i,$datum[1]))."' AND zid='".$zam['id']."' ";
                $nac_q = MySQL_Query ($nac_s);
                $nac = MySQL_Fetch_Array($nac_q);
                if (isset($nac['id'])) {
                    if (($nac['type']=='P')||($nac['type']=='IN')) {
                        echo '|';
                    } elseif($nac['type']=='OUT') {
                        echo '-';
                    } else {
                        echo $nac['type'];
                    }
                } else {
                    echo '-';
                }

                echo '</td>';
            }
            ?>
    </tr>
        <?php
    }
    ?>
</table>